<?php
/**
 * Created by PhpStorm.
 * User: Frings.Z
 * Date: 2016-05-04
 * Time: 16:17
 */

use \yii\helpers\Url;

$web = Url::to('@web');
?>

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>订单确认</title>
    <script src="<?= $web ?>/js/jquery.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/tools.js?1"></script>

    <?php require_once Yii::$app->viewPath . '/wx-no-share.php' ?>

    <?php require_once Yii::$app->viewPath . '/wx-calc-price.php' ?>

    <link href="<?= $web ?>/css/wx-good/order-tow.css" rel="stylesheet" type="text/css">
    <link href="<?= $web ?>/css/global.css" rel="stylesheet" type="text/css">
    <script>
        $(function ($) {
            var w = $(document).width();
            if (w > 640) {
                w = 640;
                console.log(w);
            }
            $("html").css("font-size", w * 20 / 320);
        });

        function addnum(me, stock) {
            var num = $("#goods_num").val();
            num = parseInt(num);

            if (num >= stock) {
                alert("库存数量不足！");
                return false;
            }

            if (num >= 99) {
                alert("买的太多了！");
                return false;
            }

            num = num + 1;
            $("#goods_num").val(num);
            $("#hidden_goods_stock").val(num);
            var totalPrice = num * parseFloat($("#goodsPrice").text());
            $("#totalPrice").text(totalPrice.toFixed(2));

            calc_coin(9007199254740991, totalPrice);
        }

        function decnum(me) {
            var num = $("#goods_num").val();
            num = parseInt(num);

            if (num <= 1) {
                alert("数量不够减了");
                return false;
            }

            num = num - 1;
            $("#goods_num").val(num);
            var totalPrice = num * parseFloat($("#goodsPrice").text());
            $("#totalPrice").text(totalPrice.toFixed(2));

            calc_coin(9007199254740991, totalPrice);
        }

        function update_goods_num(me, stock) {
            var oldNum = $("#hidden_goods_num").val();
            var newNum = $("#goods_num").val();
            newNum = parseInt(newNum);
            if (oldNum == newNum) {
                return false;
            } else if (newNum <= 0) {
                alert('数量不能小于等于0！');
                $("#goods_num").val(oldNum);
                return false;
            } else if (newNum > stock) {
                alert('库存数量不足！');
                $("#goods_num").val(oldNum);
                return false;
            } else if (newNum > 99) {
                alert('买的太多了！');
                $("#goods_num").val(oldNum);
                return false;
            }

            var totalPrice = newNum * parseFloat($("#goodsPrice").text());
            $("#totalPrice").text(totalPrice.toFixed(2));
            $("#goods_num").val(newNum);
            $("#hidden_goods_num").val(newNum);

            calc_coin(9007199254740991, totalPrice);
        }

        function submit_order() {
            //判断库存
            var buyCount = $("#goods_num").val();
            var stock = <?= $goods_Info['goods']['STOCK'] ?>;
            if (buyCount > stock) {
                alert("库存数量不足！");
                return false;
            }

            var data = {};
            //order_type = 1 代表从购物车下的订单  =2 代表单个产品快捷下单
            data.order_type = 2;
            data.coin = $('#txt_duola_coin').val();
            data.goods_code = $("#hidden_goods_code").val();
            data.count = $("#goods_num").val();
            data.phone = $("#phone").val();

            if (data.phone == "") {
                alert("请输入手机号码。");
                return false;
            }

            //判断地址是否存在
            var reAddress = requestJson("<?=Url::to(['wx-good/is-exist-address'])?>");
            if (reAddress.data.address == false) {
                window.location.href = '<?=Url::to(['wx-home/address-management', 'backurl' => Yii::$app->request->absoluteUrl])?>';
                return false;
            }


            var re = requestJson("<?=Url::to(['wx-good/order-form-handle'])?>", data);
            if (re.status == 0) {
                //跳转至订单列表
                window.location.href = "<?=Url::to(["wx-good/order-detail"]);?>?order_id=" + re.data.order_id;
            } else {
                alert(re.message);
            }
        }
    </script>
</head>
<body>
<div class="main" style="width: 16rem; margin: 0 auto;">
    <div class="head-head-top"></div>
    <div class="head">
        <div class="head-border">
            <img src="<?= $web ?>/img/wx-good/order-1.png" style="width: 100%">
        </div>
        <div class="head-null">
            <div class="user-people-null">
                <div class="user-people-img">
                    <img src="<?= $web ?>/img/wx-good/order-two-3.png" style="height: 100%">
                </div>
                <div class="user-people-name">
                    <input id="phone" type="text" maxlength="11" onkeyup='this.value=this.value.replace(/\D/gi,"")'
                           placeholder="请输入手机号码" value="">
                </div>
            </div>
        </div>

    </div>
    <div class="head-hr">
        <div>手机号用于接收消费或收货信息，不做其它用途。</div>
    </div>
    <div class="commodity">
        <div class="commodity-img">
            <?php
            $imgUrl = isset($goods_Info['goods']['GOODS_IMG_URL']) ? $goods_Info['goods']['GOODS_IMG_URL'] : "<?= $web ?>/img/wx-good/goods-default.jpg";
            ?>
            <img src="<?= $imgUrl ?>" style="width: 3rem; height: 3rem;">
        </div>
        <div class="commodity-details">
            <div class="commodity-name">
                <input id="hidden_goods_code" type="hidden" value="<?= $goods_Info['goods']['GOODS_CODE'] ?>">
                <?= $goods_Info['goods']['GOODS_NAME'] ?>
            </div>
            <div class="top-details-price">
                单价&nbsp;&nbsp;&nbsp;&nbsp;<span id="goodsPrice">  <?= $goods_Info['goods']['PRICE'] ?></span>
            </div>
            <div class="commodity-top-choice">
                <div class="choice-click">
                    <img class="reduce" src="<?= $web ?>/img/wx-good/shopping-cart-2.png" style="height: 1.4rem;">
                    <img class="reduce-2" src="<?= $web ?>/img/wx-good/shopping-cart-2-2.png" style="height: 1.4rem;"
                         onclick="decnum(this);">
                </div><!-- ——  -->
                <div class="top-choice-number">
                    <input id="hidden_goods_num" type="hidden" value="1"/>
                    <input id="hidden_goods_stock" type="hidden" value="<?= $goods_Info['goods']['STOCK'] ?>"/>
                    <input id="goods_num" type="text" placeholder="0" value="1"
                           onblur="update_goods_num(this,<?= $goods_Info['goods']['STOCK'] ?>);">
                </div>
                <div class="choice-click"><img src="<?= $web ?>/img/wx-good/shopping-cart-3.png"
                                               style="height: 1.4rem;"
                                               onclick="addnum(this,<?= $goods_Info['goods']['STOCK'] ?>);"></div>
                <!-- +  -->
            </div>
        </div>
    </div>
    <div class="price">
        <div class="price-div price-top">
            <div class="price-left">运费</div>
            <div class="price-right">0.00</div>
        </div>
        <div class="price-div price-bottom">
            <div class="price-left">总价</div>
            <div class="price-right" id="totalPrice"><?= sprintf('%.2f', $goods_Info['goods']['PRICE'] * 1) ?></div>
        </div>
    </div>
    <div class="payment">
        <div class="payment-top">
            您共有&nbsp;<span><?= $user_info['DORACOIN_ACCOUNT'] ?></span>&nbsp;朵拉币可用于支付
        </div>
        <div class="payment-bottom" style="display: block;">
            <div class="pay-integral">
                <div class="pay-left">
                    需要支付的朵拉币
                </div>
                <div class="pay-right">
                    <input id="txt_duola_coin" type="text" maxlength="8" value="0" placeholder="0"
                           oninput="checkNumVal_int(this);calc_coin(this.value);">
                </div>
            </div>
            <div class="pay-money">
                <div class="pay-money-left">
                    需要支付的现金
                </div>
                <div class="pay-money-right">
                    <span id="realPrice_dom">0</span>元
                </div>
            </div>
        </div>
        <div class="payment-bottom-two" style="display: none;">
            <div class="pay-integral">
                <div class="pay-left" style="color: #555!important;">
                    已完成支付朵拉币
                </div>
                <div class="pay-right">
                    100
                </div>
            </div>
        </div>
    </div>
    <div class="foot" onclick="submit_order();">订单确认</div>
    <div class="foot-bottom">
    </div>
</div>
</body>
</html>
